﻿using Microsoft.AspNetCore.Mvc.Filters;
using Spark.Core.Exceptions;

namespace Spark.AspNetCore.Filters
{
    public class WebApiActionFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            if (!context.ModelState.IsValid)
            {
                string message = string.Empty;
                using (var enumerator = context.ModelState.Keys.GetEnumerator())
                {
                    if (enumerator.MoveNext())
                        message = enumerator.Current + ":参数有误！";
                }
                throw new SparkException(message);
            }
            base.OnActionExecuting(context);
        }
    }
}